using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using AsmDependency.Utility;

namespace AsmDependency.UIComponents
{
   #if DEBUG
    public partial class CMainForm : CForm, ICommandHandler
    {
   #else
    public abstract partial class CMainForm : CForm, ICommandHandler
    {
   #endif
        //private AsmDependency.UIComponents.CCommandBar _commandBar;
        private AsmDependency.UIComponents.CommandManager _commandManager;
        //private CStatusBar _statusBar;

        #region Contructors
        public CMainForm(IServiceProvider serviceProvider) : base(serviceProvider)
        {
            InitializeComponent();

            this._commandManager = new AsmDependency.UIComponents.CommandManager(serviceProvider, this);
            this._commandManager.SetCommandHelpTextChangedHandler(new EventHandler(this.OnCommandHelpTextChanged));
            IServiceContainer service = (IServiceContainer) this.GetService(typeof(IServiceContainer));
            if (service != null)
            {
                service.AddService(typeof(ICommandManager), this._commandManager);
            }
        }

        //TODO: Si no complica la cosa, borrarlo
        public CMainForm() {
            if (LicenseManager.CurrentContext.UsageMode != LicenseUsageMode.Designtime)
            {
                throw new InvalidOperationException("Parameter-less constructor is meant for design-time use only.");
            }
        }

        #endregion

        #region ICommandHandler Members
        
        protected virtual bool HandleCommand(Command command)
        {
            return false;
        }

        protected virtual bool UpdateCommand(Command command)
        {
            return false;
        }

        bool ICommandHandler.HandleCommand(Command command)
        {

            return this.HandleCommand(command);
        }

        bool ICommandHandler.UpdateCommand(Command command)
        {
            return this.UpdateCommand(command);
        }
        #endregion

        #region Events
        private void OnCommandHelpTextChanged(object sender, EventArgs e)
        {
           // ((IStatusBar)this._statusBar).SetText(this._commandManager.CommandHelpText);
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            CAsyncTaskManager.RegisterUIThread(this);
        }

        protected override void OnInitialActivated(EventArgs e)
        {
            base.OnInitialActivated(e);
            ((ICommandManager)this._commandManager).ResumeCommandUpdate();
        }
        #endregion

        #region Properties
        //public CommandBar CommandBar
        //{
        //    get { return this._commandBar; }
        //    set { this._commandBar = value; }
        //}

        //public CommandManager CommandManager
        //{
        //    get { return this._commandManager; }
        //}

        //public CStatusBar StatusBar
        //{
        //    get { return this._statusBar; }
        //    set
        //    {
        //        this._statusBar = value;
        //        IServiceContainer service = (IServiceContainer)this.GetService(typeof(IServiceContainer));
        //        if (service != null)
        //        {
        //            if (this._statusBar != null)
        //            {
        //                service.AddService(typeof(IStatusBar), this._statusBar);
        //            }
        //            else
        //            {
        //                service.RemoveService(typeof(IStatusBar));
        //            }
        //        }
        //    }
        //}
        #endregion

    }
}